var util = require('../utils/util');
var daoUtil = require('../utils/daoUtil');
var knowledgeGroupClass = require('../class/knowledgegroup');

module.exports = {

    create: function (params, cb) {
        var validate = daoUtil.validate(['parentId', 'name', 'createUid', 'enable'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var knowledgegroup = new knowledgeGroupClass();
            knowledgegroup.init(params);
            models.knowledgegroup.create(knowledgegroup.getParams(), function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    update: function (params,cb) {
        var validate = daoUtil.validate(['rowId','name|enable'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        var updateItems = daoUtil.getParams(['name','enable'],params).has;
        daoUtil.proxy(function(models){
            models.knowledgegroup.update({rowId:params.rowId},updateItems).exec(function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    list: function (params, cb) {
        var that = this;
        var validate = daoUtil.validate(['parentId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.knowledgegroup.find({where:{parentId:params.parentId,enable:'1'},sort:'createdAt asc'}).populate("centers").exec(function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    }

};